const mongoose = require('mongoose');
const logger = require('../log/log_config');
const moment = require("moment");
var Dish = mongoose.model('dish');
var Merchant = mongoose.model('merchant');
var CopyFile = require('../util/copy');
const fs = require('fs');
const DishJms = require('../jms/dish_jms_producer');
const B = require('../config/base');
var base = new B();

//all dishes & un dishes
exports.loadDishes = function (req, res) {
    logger.log("info", "loadDishes time:%s", moment().format("LLLL"), "loadDishes Message:%s", JSON.stringify(req.query), {});
    if (req.query.recommend == -1) {
        Dish.find(req.query, function (err, dishes) {
            res.json(dishes);
        });
    } else {
        req.query.merchant = req.session.mid;
        Dish.find(req.query, function (err, dishes) {
            res.json(dishes);
        });
    }
}


//recommend & undercarriage
exports.updateDishRecommend = function (req, res) {
    logger.log("info", "updateDishRecommend time:%s", moment().format("LLLL"), "updateDishRecommend Message:%s", JSON.stringify({did: req.params._id}), {});
    Dish.findOne({_id: req.params._id}, function (err, dish) {
        if (dish != null && req.body.recommend != undefined) {
            dish.recommend = req.body.recommend;
            dish.save(function (err, dish2) {
                if (dish2.recommend == 1) {
                    console.log(dish2);
                    DishJms.recommendDishJMS(JSON.stringify(dish2));
                    res.json(dish2);
                }
            });
        }
    });
}


exports.deleteDish = function (req, res) {
    logger.log("info", "deleteDish time:%s", moment().format("LLLL"), "deleteDish Message:%s", JSON.stringify({did: req.params._id}), {});
    Dish.findOne({_id: req.params._id}, function (err, dish) {
        if (dish != null) {
            dish.remove(function (err) {
                res.json({});
            });
        }
    });
}

exports.addDish = function (req, res) {
    logger.log("info", "addDish time:%s", moment().format("LLLL"), "addDish Message:%s", JSON.stringify({mid: req.session.mid}), {});
    Merchant.findOne({merchant: req.session.mid}, function (err, merchant) {
        var dish = new Dish(req.body);
        dish.merchant = req.session.mid;
        dish.dicon = req.session.mid + '_' + dish.dicon;
        dish.save(function (err) {
            if (!err) {
                res.redirect('dishManager.html');
            } else {
                res.json({error: 'error add dish'});
            }
        });
    });
}

exports.uploadImg = function (req, res) {
    logger.log("info", "uploadImg time:%s", moment().format("LLLL"), "uploadImg Message:%s", JSON.stringify({dicon: req.files.file.name}), {});
    var realName = req.files.file.path;
    console.log('realName' + realName);
    if (realName != null) {
        fs.rename(realName, base.img.savePath + req.session.mid + '_' + req.files.file.name, function (err) {
            if (err) {
                throw err;
            }
        });
        CopyFile.exists(base.img.tempFile, base.img.targetFile, CopyFile.copy);
        res.json({size: 0});
    } else {
        res.json({
            size: 0
        });
    }
}




